<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Name: xiahuiqin
 * Date: 2012-4-2
 * Function: 系统公用扩展函数库
 * */

/**
 * Description: 调用后台静态文件地址
 * @param $type eg: js,css,images
 * @param $filenage eg:style.css
 * return 静态文件地址
 * */
if ( ! function_exists('admin_static_url')) {
    function admin_static_url($type = '', $filename = '') {
        return !empty($type) ? base_url('static/adminstrator/'.$type.'/'.$filename) : '';
    }
}

/**
 * Description: 调用后台静态文件地址
 * @param $type eg: js,css,images
 * @param $filenage eg:style.css
 * return 静态文件地址
 * */
if ( ! function_exists('static_url')) {
    function static_url($type = '', $filename = '') {
        return !empty($type) ? base_url(''.$type.'/'.$filename) : '';
    }
}

/**
 * Description: 调用前台控制器地址
 * @param $type eg: js,css,images
 * @param $filenage eg:style.css
 * return 静态文件地址
 * */
if ( ! function_exists('front_url')) {
    function front_url($filename = '', $type = ''){
        return !empty($type) ? base_url(''.$type.'/'.$filename) : base_url($filename);
    }
}

if(! function_exists('thumbnail')) {
    /**
     * 图片上传生成缩图
     * @param $FILES
     * @param $uploadPath
     * @param $w
     * @param $h
     * @param bool $createThumb
     * @param bool $unlinkFile
     * @return array|bool
     */
    function thumbnail($name,$FILES, $uploadPath, $w, $h, $createThumb = FALSE, $unlinkFile = FALSE) {
        $CI = &get_instance();  
        if(isset($FILES) && $FILES['name']) {
            $error = '';
            $thumbName = '';

            $config = array(
                    'upload_path' => $uploadPath,
                    'allowed_types' => 'gif|jpg|png',
            		'overwrite' => FALSE,
                    'encrypt_name' => TRUE,
                    );
            $CI->load->library('upload', $config);
            $CI->upload->initialize($config);
            if ($CI->upload->do_upload($name)) {
                //上传成功返回数组
                $data = $CI->upload->data();
                //缩图名称
                $thumbName = $createThumb ? 's_' . $data['file_name'] : $data['file_name'];
               
                if($createThumb) {
                    $config = array(
                        'image_library' => 'gd2',
                        'source_image' => $data['full_path'],
                        'maintain_ratio' => true,
                        'width' => $w,
                        'height' => $h,
                        'new_image' => $thumbName
                    );
                    $CI->load->library('image_lib');
                    $CI->image_lib->initialize($config);
                    if (!$CI->image_lib->resize()) {
                        $error = $CI->image_lib->display_errors();
                    }
                    if(file_exists($data['full_path']) && $unlinkFile) {
                        unlink($data['full_path']);
                    }
                }
            } else {
                $error =  $CI->upload->display_errors();
            }
            if($error) {
                return array('error' => $error);
            }
            return array('error' => '', 'thumbName' => $thumbName, 'fileInfo' => $data);
        }
        return FALSE;
    }
}

if(!function_exists('convert_array_to_ci_url')) {
    /**
     * Description 生成url
     * @param $urlArray
     * @return string
     */
    function convert_array_to_ci_url($urlArray) {
        if(empty($urlArray)) {
            return false;
        }
        $reuqeryUrl = '';
        foreach($urlArray as $k=>$v) {
            $reuqeryUrl .= $k.'='.$v.'&';
        }
        return site_url(rtrim($reuqeryUrl, '&'));
    }
}

//用文件存储数据操作
	function  crud_by_file($file,$data,$action="",$type = false)
	{
		$path  =  "data/".$file.'.json';
		$root = "./data";
	
		$contents = array();
		!$action && $action = 'read';
		switch ($action)
		{
			case 'update':
		 	write_file($path, json_encode($data));
		 	//echo json_encode($data);
			break;
			case 'delete':
			write_file($path, '', 'rw+');
			break;
			case 'read':
			$content = read_file($path);
            if($type == true)
            {
                !empty($content) && $contents = json_decode($content,true);
            }
            else
            {
                !empty($content) && $contents = json_decode($content);
            }
			break;
		}
		return $contents;
	}
	
//时间转换
  function convert_time($str)
   {
		$time = strtotime($str);
		$now = time();
		$time_str = "";
	    $rs =abs( $now - $time);
		//小于60秒
		if(  $rs < 60)
		{
			 $time_str = "刚刚";
		}
		else  if ( $rs > 60  && $rs < 3600)
		{
			$time_str = intval(($rs/60))."分钟前";
		}
		else if( $rs > 3600  && $rs < 3600*24)
		{
			$time_str =  intval(($rs/3600))."小时前";
		}
		else
		{
			$time_str = $str;
		}
		return $time_str;	
   }
